<template>
  <el-dialog :title="$t('finance.report.accountMonitoring.edit')" :visible.sync="showState" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="120px" class="demo-ruleForm">
      <el-form-item :label="$t('finance.report.accountMonitoring.paymentPlatform')">
        <el-input
          v-model="formValidate.platformName"
          readonly
        />
      </el-form-item>
      <el-form-item :label="$t('finance.report.accountMonitoring.balanceLimit')" prop="effectiveBalanceLimit">
        <el-input-number
          v-model="formValidate.effectiveBalanceLimit"
          :controls="false"
          :max="10000000000000"
          :placeholder="$t('finance.report.accountMonitoring.prompt.balanceLimit')"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item :label="$t('finance.report.accountMonitoring.email')" prop="email">
        <el-input v-model="formValidate.emailq" :placeholder="$t('finance.report.accountMonitoring.prompt.email')">
          <template slot="append">
            <el-select v-model="formValidate.emailh">
              <el-option
                v-for="item in $t('finance.report.accountMonitoring.emailh')"
                :key="item.value"
                :label="item.desc"
                :value="item.value"
              />
            </el-select>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item :label="$t('finance.report.accountMonitoring.state')" prop="paymentFlag">
        <el-select v-model="formValidate.paymentFlag" :placeholder="$t('productCenter.appConfig.bank.prompt.state')" style="width: 100%">
          <el-option
            v-for="item in $t('finance.report.accountMonitoring.status')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateAccountMonitoring } from '@/api/finance'
export default {
  name: 'AccountMonitoringDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    const validateEmail = (rule, value, callback) => {
      if (!this.formValidate.emailq || !this.formValidate.emailh) {
        callback(new Error(this.$t('finance.report.accountMonitoring.email') + this.$t('common.null')))
      } else {
        callback()
      }
    }
    return {
      submitLoading: false,
      formValidate: {
        id: null,
        platformName: null,
        effectiveBalanceLimit: null,
        emailq: null,
        emailh: null,
        email: null,
        paymentFlag: null
      },
      rules: {
        effectiveBalanceLimit: [{ required: true, message: this.$t('finance.report.accountMonitoring.balanceLimit') + this.$t('common.null'), trigger: 'blur' }],
        email: [{ required: true, validator: validateEmail, trigger: 'change' }],
        paymentFlag: [{ required: true, message: this.$t('finance.report.accountMonitoring.state') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    handleClose() {
      this.$emit('change-dialog', false)
      this.$refs['formValidate'].clearValidate()
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          const data = {
            id: this.formValidate.id,
            platformName: this.formValidate.platformName,
            effectiveBalanceLimit: this.formValidate.effectiveBalanceLimit,
            email: this.formValidate.emailq + this.formValidate.emailh,
            paymentFlag: this.formValidate.paymentFlag
          }
          updateAccountMonitoring(data)
            .then(res => {
              this.submitLoading = false
              if (res.success) {
                this.$message.success(this.$t('common.tip.editSuccess'))
                this.$emit('change-dialog', false)
                this.$emit('edit-success')
                this.handleClose()
              } else {
                this.$message.error(res.message)
              }
            })
            .catch(err => {
              this.$message.error(err.message)
              this.submitLoading = false
            })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-input .el-select{
    width: 170px;
  }
</style>
